//
//  HPVisitorView.swift
//  MyWeibo
//
//  Created by Apeng on 16/4/8.
//  Copyright © 2016年 Apengpeng. All rights reserved.
//

import UIKit
import SnapKit
// 申明协议
@objc protocol HPVisitorViewDelegate: NSObjectProtocol {
   optional func go2Login()
   optional func go2Register()
}

class HPVisitorView: UIView {
    
    // 声明属性, Swift中默认是强应用, 类型是HPVisitorViewDelegate类型
    weak var delegate :HPVisitorViewDelegate?
    
    

    // 重写init 初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setConstantForSubviews()
        
        setBackgroundColor()
    }
    
    
    // 对外界提供方法
    func setVisitorViewInfor(isHomePage :Bool = false, imageName :String, suggestText :String) {
        
        if isHomePage {
            startAnimation()
        } else {
            houseImageView.hidden = true
            // 移动圆圈视图到最上层
            bringSubviewToFront(iconImageView)
        }
        iconImageView.image = UIImage(named: imageName)
        suggestLabel.text = suggestText
    }
    
    // 提示不能通过xib/sb 加载视图
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 登录注册按钮点击
    @objc private func registBtnDidClick() {
        print("registBtn")
        // go2Register? 可实现也可不实现
        delegate?.go2Register?()
    }
    
    @objc private func loginBtnDidClick() {
        print("loginBtn")
        delegate?.go2Login?()
        
    }
    
    // 设置动画
    private func startAnimation() {
        let anim = CABasicAnimation()
        anim.keyPath = "transform.rotation"
        anim.toValue = 2 * M_PI
        anim.duration = 15
        anim.repeatCount = MAXFLOAT
        iconImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    // 设置背景颜色
    private func setBackgroundColor() {
        
        let colorValue :CGFloat = 237 / 255.0
        backgroundColor = UIColor(red: colorValue, green: colorValue, blue: colorValue, alpha: 1.0)
    }
    
    // 设置约束
    private func setConstantForSubviews() {
        
        addSubview(iconImageView)
        //需要放在这个位置, 否则无法遮盖
        addSubview(cover)
        addSubview(houseImageView)
        addSubview(suggestLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        
        // 使用SnapKit 框架
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80.0)
        }
        
        houseImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(iconImageView.snp_center)
        }
        
        suggestLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
            make.centerX.equalTo(iconImageView.snp_centerX)
            make.width.equalTo(240)
            make.height.equalTo(70)
            
        }
        
        registBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(suggestLabel.snp_centerX).offset(10)
            make.top.equalTo(suggestLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(suggestLabel.snp_centerX).offset(-10)
            make.top.equalTo(suggestLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
        }
        
        cover.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(houseImageView.snp_center)
        }
    }
    
    // 设置房子的遮盖
    private lazy var cover :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
   // 懒加载控件
    private lazy var iconImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var houseImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var suggestLabel :UILabel = {
      
        let suggestLabel = UILabel()
        suggestLabel.text = "登录微博\n你会发现世界发生巨大变化\n登上微信\n你会发现生活只有秀恩爱晒结晶"
        suggestLabel.numberOfLines = 0
        suggestLabel.textAlignment = .Center
        suggestLabel.textColor = UIColor.darkGrayColor()
        suggestLabel.font = UIFont.systemFontOfSize(14)
        // 自适应大小
        suggestLabel.sizeToFit()
        
        return suggestLabel
    }()
    
    private lazy var registBtn :UIButton = {
       
        let registBtn = UIButton()
        registBtn.setTitle("注册", forState: .Normal)
        registBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        registBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        let img = UIImage(named: "common_button_white_disable")!
        let imgW = Int(img.size.width * 0.5)
        let imgH = Int(img.size.height * 0.5)
        registBtn.setBackgroundImage(img.stretchableImageWithLeftCapWidth(imgW, topCapHeight: imgH), forState: .Normal)
        registBtn.addTarget(self, action: "registBtnDidClick", forControlEvents: .TouchUpInside)
        return registBtn
    }()
    
    private lazy var loginBtn :UIButton = {
        
        let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: .Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        loginBtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        let img = UIImage(named: "common_button_white_disable")!
        let imgW = Int(img.size.width * 0.5)
        let imgH = Int(img.size.height * 0.5)
        
        loginBtn.setBackgroundImage(img.stretchableImageWithLeftCapWidth(imgW, topCapHeight: imgH), forState: .Normal)
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        return loginBtn
    }()
    
}
